﻿using UnityEngine;
using System.Collections;

public class GUIManager : MonoBehaviour {
	
	private static GUIManager instance;
	public GameObject spawnbox;
	public GUIStyle omg;
	public GUIStyle omg2;
	public GUIText boostsText, distanceText, gameOverText, instructionsText, instructionsText1, instructionsText2, instructionsText3,runnerText;
	public bool noGUI;
	public GameObject[] bubbles;
	public GameObject creditsgui;
	public GameObject regulargui;
    public GameObject highgui;
	public GameObject myBubbleB;
	public GameObject myBubbleC;
    public GameObject myBubbleH;
    public GameObject newHigh;
	public bool onlyStartBool;
        

	public float mystartgametimer;
	public static bool startgame;
    private RaycastHit hit;
	void Start () {
		Screen.orientation = ScreenOrientation.LandscapeLeft;
		startgame = true;
		onlyStartBool = true;
		myBubbleB.gameObject.SetActive(false);
		creditsgui.gameObject.SetActive(false);
        highgui.gameObject.SetActive(false);
        newHigh.gameObject.SetActive(false);
        instance = this;
		GameEventManager.GameStart += GameStart;
		GameEventManager.GameOver += GameOver;
		gameOverText.enabled = false;
		noGUI = false;

		bubbles = new GameObject[] {myBubbleB, myBubbleC, myBubbleH};
		
		foreach(GameObject go in bubbles){
			go.renderer.enabled = true;
		}

	}

	void Update () {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hit))
        {
            switch(hit.transform.tag)
            {
                case "credits":
       			    creditsgui.gameObject.SetActive(true);
			        regulargui.gameObject.SetActive(false);
			        myBubbleC.SetActive(false);
                    myBubbleB.SetActive(true);
                    highgui.SetActive(false);
                    newHigh.gameObject.SetActive(false);
                    break;
                case "highscore":
                    for (int i = 1; i < 6; i++)
                    {
                        highgui.transform.GetChild(0).transform.GetChild(i - 1).guiText.text = PlayerPrefs.HasKey("HighScore" + i) ? string.Format("{0}.{1, 30}", i, PlayerPrefs.GetInt("HighScore" + i).ToString()) :  "";
                    }
                    creditsgui.gameObject.SetActive(false);
                    regulargui.gameObject.SetActive(false);
                    myBubbleC.SetActive(false);
                    myBubbleB.SetActive(true);
                    myBubbleH.SetActive(false);
                    highgui.SetActive(true);
                    noGUI = true;
                    newHigh.gameObject.SetActive(false);
                    break;
                case "back":
                    myBubbleC.SetActive(true);
                    myBubbleH.SetActive(true);
                        myBubbleB.SetActive(false);
                    creditsgui.gameObject.SetActive(false);
                    regulargui.gameObject.SetActive(true);
                    highgui.SetActive(false);
                    newHigh.gameObject.SetActive(false);
                    break;
                default:
  				    creditsgui.gameObject.SetActive(false);
				    regulargui.gameObject.SetActive(true);
                    highgui.SetActive(false);
				    GameEventManager.TriggerGameStart();
				    noGUI = true;
                    newHigh.gameObject.SetActive(false);
                    break;
            }
        }

		if(!startgame){
		//	mystartgametimer = 0;
				mystartgametimer += Time.deltaTime;
			if(mystartgametimer > .3f){
				startgame = true;
				mystartgametimer = 0;
			}
		}
		//DontDestroyOnLoad(transform.gameObject);
	}
	void OnGUI(){
	

	}
	
	private void GameStart () {
		
		foreach(GameObject go in bubbles){
			go.renderer.enabled = false;
		}
		gameOverText.enabled = false;
		instructionsText.enabled = false;
		instructionsText1.enabled = false;
		instructionsText2.enabled = false;
		instructionsText3.enabled = false;

		runnerText.enabled = false;
		enabled = false;

		Spawner spwnRef = spawnbox.GetComponent<Spawner>();
		spwnRef.deathTime = 0;
		spwnRef.newtime = 0;
		spwnRef.timer = 0;
		GameObject[] sharkRef = GameObject.FindGameObjectsWithTag("shark");
		foreach(GameObject GO in sharkRef){
			Destroy(GO);
		}
	}
	
	private void GameOver () {
        newHigh.gameObject.SetActive(PlayerPrefs.GetInt("NewHigh") == 1 ? true : false);

		noGUI = false;
		foreach(GameObject go in bubbles){

			go.renderer.enabled =true;
		}
		gameOverText.enabled = true;
		instructionsText.enabled = true;
		instructionsText1.enabled = true;
		instructionsText2.enabled = true;
		instructionsText3.enabled = true;
		enabled = true;

	}
	
	public static void SetBoosts(int boosts){
		instance.boostsText.text = boosts.ToString();
	}

	public static void SetDistance(float distance){
		instance.distanceText.text = Mathf.CeilToInt(distance).ToString();
	}
}